﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class StockEdit : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDL();
                BindData();
            }
            Master.ValidationGroup = "gSubmit";
        }

        private void InitDDL()
        {
            BindParameterForListControl("StockType", ddlStockType);
            //UIHelper.InsertFirstItem(ddlStockType);
        }
      
        private void BindData()
        {
            string stockid = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(stockid))
            {
                hdStockID.Value = stockid;
                MdStockEntity en = MdStock.GetMdStockByID(stockid);
                if (en != null)
                {
                    hdStockID.Value = stockid;
                    txtStockID.Text = stockid;
                    txtStockName.Text = en.StockName;
                    ddlStockType.SelectedValue = en.StockType;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写仓库类别资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.txtStockName.Text.Trim()))
                {
                    if ( (new MdStock()).IsNameExists(this.txtStockName.Text.Trim(), hdStockID.Value.Trim()))
                    {
                        UIHelper.AlertMessage(this.Page, "仓库类别名称已存在，请重新填写！");
                        return;
                    }
                    MdStockEntity entity = new MdStockEntity();
                    entity.StockID =GetString(txtStockID);
                    entity.StockName = GetString(txtStockName);
                    entity.Status = "1";
                    entity.ModifyDate = DateTime.Now;
                    entity.ModifyUserID = CurrentUser.UserID;
                    entity.ModifyUserName = CurrentUser.UserName;

                    if (MdStock.Update(entity))
                    {
                        base.LogAction(BaseEnum.ActionType.Modify, "", true, null, new EntityBase[] { entity });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/StockList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}